/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Demos;

import org.osdtsystem.dataset.DataPoint;
import org.osdtsystem.dataset.DataPointWeighted;
import org.osdtsystem.dataset.Dataset;
import org.osdtsystem.dataset.DatasetRaw;
import org.osdtsystem.dataset.DatasetSubset;
import org.osdtsystem.utils.MemoryStorage;
import org.osdtsystem.utils.Storage;

/**
 * Demo showing how to create datasets.
 * @author Matthias Buch-Kromann
 */
public class DatasetDemo {
    static Storage storage = new MemoryStorage();
    /**
     * Static example 1 of a raw dataset.
     */
    public static Dataset datasetRaw1 = datasetRaw1(), datasetRaw2 = datasetRaw2();

    /**
     * Example 1 of a raw dataset.
     * @return raw dataset
     */
    public static Dataset datasetRaw1() {
        Dataset dataset = new DatasetRaw(3, storage);
        for (int i = 0; i < 10; ++i) {
            dataset.append(new DataPoint("A" + i, "B" + i, "C" + i));
        }
        return dataset;
    }

    /**
     * Example 2 of a raw dataset with weighted data points. Non-static.
     * @return raw dataset
     */
    public static Dataset datasetRaw2() {
        Dataset dataset = new DatasetRaw(3, storage);
        for (int i = 0; i < 10; ++i) {
            dataset.append(new DataPointWeighted(2, "A" + i, "B" + i, "C" + i));
        }
        return dataset;
    }

    /**
     * Non-static example 1 of a subset of datasetRaw1.
     * @return subset of datasetRaw1
     */
    public static Dataset datasetSubset1() {
        return new DatasetSubset(datasetRaw1, 1, 3, 5, 7, 9);
    }

    /**
     * Non-static example 2 of a subset of datasetRaw1
     * @return subset of datasetRaw1
     */
    public static Dataset datasetSubset2() {
        return new DatasetSubset(datasetRaw1, 2, 3, 4);
    }

    /**
     * Non-static example 2 of a subset of datasetRaw1
     * @return subset of datasetRaw1
     */
    public static Dataset datasetSubset3() {
        return new DatasetSubset(datasetRaw1, 3);
    }

    /**
     * Non-static example 2 of a subset of datasetRaw1
     * @return subset of datasetRaw1
     */
    public static Dataset datasetSubset4() {
        return new DatasetSubset(datasetRaw1, 9);
    }

    /**
     * A string representation of the identifiers in a dataset.
     * @param dataset the dataset
     * @return a string with the space-separated identifiers in the dataset
     */
    public static String datasetIdentifiers(Dataset dataset) {
        StringBuilder s = new StringBuilder();
        boolean first = true;
        for (Integer i : dataset) {
            if (! first)
                s.append(" ");
            first = false;
            s.append(i);
        }
        return s.toString();
    }


    /**
     * A string representation of a dataset. The resulting string encodes
     * the dimensionality, size, and the individual data points.
     * @param dataset the dataset
     * @return a string representation of the dataset
     */
    public static String datasetToString(Dataset dataset) {
        StringBuilder s = new StringBuilder();
        s.append("Dataset[dim=");
        s.append(dataset.dimensionality());
        s.append(",size=");
        s.append(dataset.size());
        s.append("]\n");
        for (int i = 0; i < dataset.size(); ++i) {
            DataPoint d = dataset.dataPoint(i);
            s.append("  ");
            s.append(i);
            s.append(": ");
            s.append(d.toString());
            s.append("\n");
        }
        return s.toString();
    }
}
